﻿using System;
using System.ApplicationModel;
using System.ApplicationModel.Composition;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Anagramme.Services;
using Anagramme.Views;

namespace Anagramme.ViewModels
{
    /// <summary>
    /// ViewModel du service de messages
    /// </summary>
    [Export(typeof(IMessageService))]
    [ViewModelExport(Modules.Message)]
    public sealed class MessageViewModel : ViewModelBase, IMessageService
    {
        #region Fields
        private string _text;
        private MessageBoxImage _icon;
        private ICommand _okCommand;
        private IWindow _view;
        #endregion

        #region Properties
        /// <summary>
        /// Obtient l'icône à affichier
        /// </summary>
        public MessageBoxImage Icon
        {
            get { return this._icon; }
            private set
            {
                if (this._icon != value)
                {
                    base.RaisePropertyChanging();
                    this._icon = value;
                    base.TraceAssign(value);
                    base.RaisePropertyChanged();
                }
            }
        }

        /// <summary>
        /// Obtient le texte à affichier
        /// </summary>
        public string Text
        {
            get { return this._text; }
            private set
            {
                if (this._text != value)
                {
                    base.RaisePropertyChanging();
                    this._text = value;
                    base.TraceAssign(value);
                    base.RaisePropertyChanged();
                }
            }
        }
        #endregion

        #region Commands
        /// <summary>
        /// Obtient la commande pour fermer la fenêtre
        /// </summary>
        public ICommand OkCommand
        {
            get
            {
                if (object.ReferenceEquals(this._okCommand, null))
                {
                    this._okCommand = new DelegateCommand(this.Close);
                }
                return this._okCommand;
            }
        }
        #endregion

        #region IMessageService Members
        /// <summary>
        /// Affiche un message d'erreur
        /// </summary>
        /// <param name="message">Message à afficher</param>
        public void ShowError(string message)
        {
            ApplicationServices.Trace.TraceMethodEntering();

            this.Text = message;
            this.Icon = MessageBoxImage.Error;
            this._view = ApplicationServices.Container.GetView<IWindow>(Modules.Message);
            this._view.ShowDialog();

            ApplicationServices.Trace.TraceMethodLeaving();
        }

        /// <summary>
        /// Affiche un message d'information
        /// </summary>
        /// <param name="message">Message à afficher</param>
        public void ShowInformation(string message)
        {
            ApplicationServices.Trace.TraceMethodEntering();

            this.Text = message;
            this.Icon = MessageBoxImage.Information;
            this._view = ApplicationServices.Container.GetView<IWindow>(Modules.Message);
            this._view.ShowDialog();

            ApplicationServices.Trace.TraceMethodLeaving();
        }
        
        /// <summary>
        /// Affiche un message
        /// </summary>
        /// <param name="message">Message à afficher</param>
        public void ShowMessage(string message)
        {
            ApplicationServices.Trace.TraceMethodEntering();

            this.Text = message;
            this.Icon = MessageBoxImage.None;
            this._view = ApplicationServices.Container.GetView<IWindow>(Modules.Message);
            this._view.ShowDialog();

            ApplicationServices.Trace.TraceMethodLeaving();
        }

        /// <summary>
        /// Affiche un message d'avertissement
        /// </summary>
        /// <param name="message">Message à afficher</param>
        public void ShowWarning(string message)
        {
            ApplicationServices.Trace.TraceMethodEntering();

            this.Text = message;
            this.Icon = MessageBoxImage.Warning;
            this._view = ApplicationServices.Container.GetView<IWindow>(Modules.Message);
            this._view.ShowDialog();

            ApplicationServices.Trace.TraceMethodLeaving();
        }
        #endregion

        #region Methods
        private void Close()
        {
            ApplicationServices.Trace.TraceMethodEntering();

            this._view.Close();

            ApplicationServices.Trace.TraceMethodLeaving();
        }
        #endregion
    }
}
